home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 11471 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.9 KB  |  63 lines

  1. Path: ts1-000.jaxnet.com!user
  2. From: garyg@jax.jaxnet.com (Gary M. Greenberg)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Newbie needs help w/ recursion
  5. Date: 24 Mar 1996 17:37:23 GMT
  6. Organization: Southeast Network Services, Inc.
  7. Message-ID: <garyg-2403961228170001@ts1-000.jaxnet.com>
  8. References: <4ikq6p$io1@impsets.dash.com> <garyg-2403961218500001@ts1-000.jaxnet.com>
  9. NNTP-Posting-Host: ts1-000.jaxnet.com
  10.  
  11. He inanely wrote:
  12.  
  13. >  In article <4ikq6p$io1@impsets.dash.com>, Jim Bosshardt <jbossha@dash.com>
  14. >  wrote:
  15. >  
  16. >  >  Hi, I am new and am about to pull my hair out over the supposedly simple 
  17. >  >  recursion problem I have. The function is to take a number and raise it 
  18. >  >  to a neg or pos power and return the value to main().  I need to take 
  19. >  >  the following and make it a recursive function...
  20. >  >  
  21. >  [snip]
  22. >  
  23. >  When I was very new to C programming, I wrote this one to solve the
  24. >  problem in the Waite Group's New C Primer Plus. This sounds familiar.
  25. >  I've not revisited the code but here was my function ...
  26. >  
  27. [SNIP <Cancel issued on posting, but likely not effective>]
  28.  
  29. Clearly non-recursive solution posted; sorry.
  30.  
  31. I grabbed the wrong 'clipboard'
  32.  
  33. the recursive solution that I used was:
  34.  
  35. /* ïïïïïïïïï r_do_power function ïïïïïïïï */
  36. double r_do_power (double x,int y)
  37. {
  38.     double r_pow=x;
  39.     if(x==0)
  40.         r_pow=0;
  41.     if(x!=0 && y==0)
  42.         r_pow=1;
  43.     if((x>0 || x<0) && y>0)
  44.         r_pow = r_pow * r_do_power(x,y - 1);
  45.  
  46.     if((x>0 || x<0) && y < 0)
  47.         r_pow = 1/r_pow * 1/r_do_power(1/x,y + 1);
  48.  
  49.     return r_pow;
  50. }
  51. /*** WARNING *** WARNING *** WARNING ***/
  52. Be advised that recursive calls for even rather small powers often result
  53. in crashes due to stack overload.
  54. /*** caveat emptor ***/
  55.  
  56. C'ya,
  57.  
  58. gary    /* the Sorcerer's Apprentice */
  59.     http:/jax.jaxnet.com/~garyg/main_page.html
  60.  
  61. Free Speech in America murdered by the Communications Decency Act.
  62.       (((U.S. Congress && Executive) == (Collective Moron)) == 1)
  63.